package com.csdemos.algs.graphs;

import java.util.LinkedList;

public class AdjacencyMatrixGraph implements Graph {
	
	private int[][] A;
	private int[][] W;
	private int[] V;
	
	public AdjacencyMatrixGraph(int n) {
		A = new int[n][n];
		W = new int[n][n];
		V = new int[n];
		for (int i = 0; i < n; i++) {
			V[i] = i;
		}
	}
	
	@Override
	public void addEdge(int v1, int v2) {
		A[v1][v2] = 1;
	}
	
	@Override
	public void addEdge(int v1, int v2, int weight) {
		A[v1][v2] = 1;
		W[v1][v2] = weight;
	}
	
	@Override
	public LinkedList<Integer> getAdjecents(Integer vertex) {
		LinkedList<Integer> result = new LinkedList<Integer>();
		for (int j = 0; j < V.length; j++) {
			if (A[vertex][j] == 1) {
				result.add(j);
			}
		}
		return result;
	}
	
	public int[] getVertexs() {
		return V;
	};
	
	@Override
	public Graph transpose() {
		int n = V.length;
		AdjacencyMatrixGraph graph = new AdjacencyMatrixGraph(n);
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < i; j++) {
				graph.A[i][j] = A[j][i];
				graph.A[j][i] = A[i][j];
				graph.W[i][j] = W[j][i];
				graph.W[j][i] = W[i][j];
			}
		}
		return graph;
	}

	@Override
	public int getWeight(int v1, int v2) {
		return W[v1][v2];
	}
}
